<?php

// function get_UniUser_by_id($id) {
// 	$customerId = Session::$user['customerId'];
// 	Session::switchCustomer(0);
	
// 	$uniUserModel = M('uni_user');
// 	$uniUser = $uniUserModel->where("id='" . $id ."'")->find();
	
// 	Session::switchCustomer($customerId);
	
// 	return $uniUser;
// }


// function get_UniUser_by_userName($userName) {
// 	$customerId = Session::$user['customerId'];
// 	Session::switchCustomer(0);
	
// 	$uniUserModel = M('uni_user');
// 	$uniUser = $uniUserModel->where("fromUserName='" . $userName ."' AND customerId='" . $customerId . "'")->find();
	
// 	Session::switchCustomer($customerId);
	
// 	return $uniUser;
// }


// function get_UniUserInfo_by_phone($phone) {
// 	$customerId = Session::$user['customerId'];
// 	Session::switchCustomer(0);

// 	$uniUserInfoModel = M('uni_user_info');
// 	$uniUserInfo = $uniUserInfoModel->alias('info')
// 	->join("wx_uni_user  user on user.uniUserId = info.uniUserId")
// 	->where("info.phone='" . $phone ."' AND user.customerId='" . $customerId . "'")
// 	->field("info.*")->find();

// 	Session::switchCustomer($customerId);

// 	return $uniUserInfo;
// }
// function create_UniUser_by_nullUserName(){
// 	$customerId = Session::$user['customerId'];
// 	Session::switchCustomer(0);
// 	$uniUserId = Encryption::uuid();

// 	$uniUserModel = M('uni_user');

// 	$uniUserModel->create();
// 	$uniUserModel->fromUserName = '';
// 	$uniUserModel->uniUserId = $uniUserId;
// 	$uniUserModel->customerId = $customerId;
// 	$uniUserModel->add();

// 	Session::switchCustomer($customerId);
// 	return $uniUserId;
// }

// function create_UniUserInfo_by_phone($uniUserId,$phone,$fullName=NULL){
// 	$customerId = Session::$user['customerId'];
// 	Session::switchCustomer(0);
	
// 	$uniUserInfoModel = M('uni_user_info');
	
// 	$uniUserInfoModel->create();
// 	$uniUserInfoModel->uniUserId = $uniUserId;
// 	$uniUserInfoModel->phone = $phone;
// 	$uniUserInfoModel->fullName = $fullName;
// 	$uniUserInfoModel->lastPostTime = date('Y-m-d H:i:s');
// 	$uniUserInfoModel->add();

// 	Session::switchCustomer($customerId);
// 	return $uniUserId;
// }
	

///////////////
////////////

function update_UserInfo_by_message($userId,$content,$userInfoField){
	$customerId = Session::$user['customerId'];
	Session::switchCustomer($customerId);
	$UserInfoModel = M('user_info');

	$userInfo = get_UserInfo_by_fromUserName($userId);
	if(isset($userInfo)){
		$userInfo [$userInfoField] = $content;
		$userInfo['lastPostTime'] = date('Y-m-d H:i:s');
		$result = $UserInfoModel->where(['id' => $userInfo['id']])->save ( $userInfo );
		// 		Log::write($UniUserInfoModel->getLastSql());
		
	}else{
	    $data['uid'] = $userId;
	    $data['lastPostTime'] = date('Y-m-d H:i:s');
	    $data[$userInfoField] = $content;
//		$UserInfoModel->create();
//		$UserInfoModel->fromUserName = $userId;
//		$UserInfoModel->lastPostTime = date('Y-m-d H:i:s');
//		$UserInfoModel->$userInfoField = $content;
		$result = $UserInfoModel->add($data);
	}
	if (!$result) {
		Log::write('update_UserInfo_by_message failed, userId:' . $userId . ',' .
				$userInfoField . ':' . $content);
		Log::write($UserInfoModel->getDbError());
	}

}

function get_UserInfo_by_fromUserName($userId){
	$customerId = Session::$user['customerId'];

	$UserInfoModel = M('user_info');
	$userInfo = $UserInfoModel->where("uid='" . $userId ."'")->find();
	return $userInfo;

}
//��ȡ�û�Ĭ���ջ���ַ
function get_User_Address_fromUserName($userId,$isAcitivity){
	$customerId = Session::$user['customerId'];
	$UserAddress = M('user_receiver_address');
	$where["uid"] = $userId;
	$where["isDefault"] = 1;
	$where["deleted"] = 0;
	if($isAcitivity == 1){
		$where["isAcitivity"] = 1;
	}
	$useraddress = $UserAddress->where($where)->select();
	return $useraddress;
}
//��ȡ�û���ַ�б�
function get_User_Address_List_fromUserName($fromUserName){
	$customerId = Session::$user['customerId'];
	$UserAddress = M('user_receiver_address');
	$useraddress = $UserAddress->where(["userName"=>$fromUserName,"deleted"=>0])->select();
	return $useraddress;
}

function create_UserInfo_by_fromUserName($fromUserName){
	$customerId = Session::$user['customerId'];

	$UserInfoModel = M('user_info');

	$UserInfoModel->create();
	$UserInfoModel->fromUserName = $fromUserName;
	$UserInfoModel->lastPostTime = date('Y-m-d H:i:s');
	$UserInfoModel->add();
	
}

// function get_UserInfo_by_userName($userName) {
// 	$customerId = Session::$user['customerId'];
// 	// 	Session::switchCustomer(0);

// 	$UserModel = M('user');
// 	$userInfo = $UserModel->alias("user")
// 	->join("wx_".$customerId."_user_info info ON user.userName = info.fromUserName")
// 	->where("user.fromUserName='$userName' AND user.customerId=$customerId")
// 	->field("user.userName as oldUserName, info.*")->find();

// 	if ($userInfo["oldUserName"] != NULL && $userInfo["fromUserName"] == NULL) {
// 		$userInfo["fromUserName"] = $userInfo["oldUserName"];
// 	}
// 	// 	print_r($uniUserInfo);
// 	// 	Session::switchCustomer($customerId);

// 	return $userInfo;
// }

/////////////////
////////////////////


function get_Member_UniUserInfo_by_userName($userName) {
	$customerId = Session::$user['customerId'];
	Session::switchCustomer($customerId);

	$MemberUniUserModel = M('member_uni_user_info');
	$condition['fromUserName'] = array('eq',$userName);
	//$condition['customerId'] = array('eq',$customerId);
	$memberUserInfo = $MemberUniUserModel->where($condition)->find();
	Session::switchCustomer($customerId);
	return $memberUserInfo;
}

function get_Member_UniUserInfo_by_phone($phone) {
	$customerId = Session::$user['customerId'];
	$userName = Session::$user['userName'];
	Session::switchCustomer(0);

	$uniUserInfoModel = M('member_uni_user_info');
	$condition['fromCustomerId'] = array('eq',$customerId);
	$condition['fromUserName'] = array('eq',$userName);
	$condition['phone'] = array('eq',$phone);
	$uniUserInfo = $uniUserInfoModel
	->where($condition)
	->find();

	Session::switchCustomer($customerId);

	return $uniUserInfo;
}

// function create_Member_UniUserInfo_by_uniUserId($userName){
// 	$customerId = Session::$user['customerId'];
// 	Session::switchCustomer(0);

// 	$uniUserInfoModel = M('member_uni_user_info');

// 	$uniUserInfoModel->create();
// 	$uniUserInfoModel->fromCustomerId = $customerId;
// 	$uniUserInfoModel->fromUserName = $userName;
// 	$uniUserInfoModel->lastPostTime = date('Y-m-d H:i:s');
// 	$uniUserInfoModel->add();

// 	Session::switchCustomer($customerId);
// }

function create_Member_UniUserInfo_by_fromUserName($userName){
	$customerId = Session::$user['customerId'];
	Session::switchCustomer($customerId);

	$uniUserInfoModel = M('member_uni_user_info');

	$uniUserInfoModel->create();
	//$uniUserInfoModel->fromCustomerId = $customerId;
	$uniUserInfoModel->fromUserName = $userName;
	$uniUserInfoModel->lastPostTime = date('Y-m-d H:i:s');
	$uniUserInfoModel->add();

	Session::switchCustomer($customerId);
}

function update_Member_UniUserInfo_by_message($userName,$content,$userInfoField){
	$customerId = Session::$user['customerId'];
	Session::switchCustomer($customerId);
	$UniUserInfoModel = M('member_uni_user_info');

	$uniUserInfo = get_Member_UniUserInfo_by_userName($userName);
	if(isset($uniUserInfo)){
		$uniUserInfo [$userInfoField] = $content;
		$uniUserInfo['lastPostTime'] = date('Y-m-d H:i:s');
		$UniUserInfoModel->save ( $uniUserInfo );
		// 		Log::write($UniUserInfoModel->getLastSql());
	}else{
		$UniUserInfoModel->create();
		//$UniUserInfoModel->fromCustomerId = $customerId;
		$UniUserInfoModel->fromUserName = $userName;
		$UniUserInfoModel->lastPostTime = date('Y-m-d H:i:s');
		$UniUserInfoModel->$userInfoField = $content;
		$UniUserInfoModel->add();
	}

	// 	Log::write('update uniuserinfo sql is:'.$UniUserInfoModel->getLastSql());
	Session::switchCustomer($customerId);
}
